home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_300
/
339_01
/
cldemo.c
< prev
next >
Wrap
Text File
|
1990-12-12
|
47KB
|
1,517 lines
/* CLDEMO.C - Demo program for THE CTRL+C LIBRARY functions */
/*
** CTRLCLIB - The CTRL+C Library(Tm)
** Copyright 1990 Trendtech Corporation, Inc.
** All Rights Reserved Worldwide
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#ifdef __TURBOC__
#define CLEAR_SCREEN clrscr()
#define _outtext(x) printf("%s",(x))
#define _settextposition(r,c) gotoxy(c,r) /* yes, Microsoft has it backwards */
#define _settextwindow(r1,c1,r2,c2) window(r1,c1,r2,c2)
#define getch getche
#define DoTimeDelay(x) delay(x + 50);
#else
#include <graph.h>
#define CLEAR_SCREEN _clearscreen(_GCLEARSCREEN)
#endif
#include <dos.h>
#include <bios.h>
#include <time.h>
#include <string.h>
#include "ctrlc.h" /* <-- this header must be in every source module */
/* calling a CTRLCLIB function. */
/* some neat boolean logic conditions */
#define FALSE 0
#define TRUE ~FALSE
#define NO FALSE
#define YES ~NO
#define OFF FALSE
#define ON ~OFF
#define HELL_IS_BURNING 1
#define QUIT (((char)c=='q')||((scn==0x10)&&((char)c=='Q')))
char *cr[]={"CTRLCLIB - The Ctrl+C Library(Tm)",
"Copyright 1990 Trendtech Corporation, Inc.",
"All Rights Reserved Worldwide" };
/*
===================================================================
= Function Prototypes
===================================================================
*/
int TEST1(void);
int TEST2(void);
int TEST3(void);
int TEST4(void);
int TEST5(void);
int TEST6(void);
int TEST7(void);
int TEST8(void);
int TEST9(void);
int TEST10(void);
int TEST11(void);
int TEST12(void);
int TEST13(void);
int TEST14(void);
int TEST15(void);
int TEST16(void);
int TEST17(void);
int TEST18(void);
int TEST19(void);
int TEST20(void);
int TEST21(void);
/*
===================================================================
= Function Prototypes
===================================================================
*/
static void header(char *);
static void footer(void);
#ifndef __TURBOC__
static unsigned DoTimeDelay(int);
#endif
static unsigned int ReadChar( char * );
static char *spc(void);
void IntroScreen(void);
void FinalScreen(void);
/*
===================================================================
= Variables
===================================================================
*/
#ifndef __TURBOC__
struct dostime_t t, *pt;
time_t tm;
#endif
char s[80], *sp;
unsigned int c, i, scn;
/*
===================================================================
= M A I N
===================================================================
*/
void main(void)
{
#ifdef __TURBOC__
delay(0);
#else
time(&tm);
srand((unsigned)tm);
#endif
IntroScreen();
while(HELL_IS_BURNING)
{
if (TEST1() == 'Q') break;
if (TEST2() == 'Q') break;
if (TEST3() == 'Q') break;
if (TEST4() == 'Q') break;
if (TEST5() == 'Q') break;
if (TEST6() == 'Q') break;
if (TEST7() == 'Q') break;
if (TEST8() == 'Q') break;
if (TEST9() == 'Q') break;
if (TEST10() == 'Q') break;
if (TEST11() == 'Q') break;
if (TEST12() == 'Q') break;
if (TEST13() == 'Q') break;
if (TEST14() == 'Q') break;
if (TEST15() == 'Q') break;
if (TEST16() == 'Q') break;
if (TEST17() == 'Q') break;
if (TEST18() == 'Q') break;
if (TEST19() == 'Q') break;
if (TEST20() == 'Q') break;
if (TEST21() == 'Q') break;
break;
}
CLEAR_SCREEN;
FinalScreen();
exit(0);
}
/*********************************************************************/
int TEST1(void)
{
static char far *txt[]={
"This test illustrates COMPLETE trapping of the infamous ^C break.\n",
"Using functions from CTRLCLIB, you can trap the <CTRL>+<C> and\n",
"<CTRL>+<BREAK> keystrokes, as well as eliminate the ^C characters from\n",
"echoing to STDOUT when either of these keys are pressed by the user.\n\n",
" PRESS q to quit this test, or Q to quit the demo...\n",
""};
c = i = 0;
CLEAR_SCREEN;
header("TEST 1 - Demonstrate Full CTRL+C and CTRL+BREAK trapping\n\n");
do { printf("%s",txt[i]); } while (*txt[++i]);
printf("\nPress any key, or try <CTRL>+<C> -or- <CTRL>+<BREAK>...\n\n");
FullCtrlC(DISABLE); /* <<--*** CTRLCLIB Function *** */
while(1)
{
if ( (i = QueryCtrlC()) != 0 ) /* <<--*** CTRLCLIB Function *** */
{
switch (i)
{
case CTRLC:
printf(" You pressed the <CTRL>+<C> keys\n");
break;
case CTRLBREAK:
printf(" You pressed the <CTRL>+<BREAK> keys\n");
break;
case CTRL2:
printf(" You pressed the <CTRL>+<2> keys\n");
break;
case ALT3:
printf(" You pressed the <ALT>+<kp-3> keys\n");
break;
default:
printf(" Whoops, this should never happen!!\n");
break;
}
printf(" Press any key...\n");
continue;
}
else
if ( !kbhit() ) continue;
if ( (c = ReadChar(s)) == 0) /* read and print the char typed */
continue; /* (not a printable char) */
printf(s);
if (QUIT) break;
}
FullCtrlC(ENABLE); /* <<--*** CTRLCLIB Function *** */
if (c != 'Q')
{
printf("\nCTRL+C and CTRL+BREAK have been ENABLED; NOW press either set of keys to\n");
printf("'break' out of this demo program if you wish; -or- press any key to\n");
printf("continue the next test...");
getch();
}
return(c);
}
/*********************************************************************/
int TEST2(void)
{
static char far *txt[]={
"This test illustrates how DOS normally echos a ^C to the screen when\n",
"either <CTRL>+<C> or <CTRL>+<BREAK> is pressed. Control-break is NOT\n",
"trapped in this test, so it too acts like control-c. \n\n",
" PRESS q to quit this test, or Q to quit demo...\n",
""};
c = i = 0;
CLEAR_SCREEN;
header("TEST 2- Disable CTRL+C and illustrate ^C using DOS print functions");
do { printf("%s",txt[i]); } while (*txt[++i]);
_settextposition(15,1);
printf("Now press any key, or try <CTRL>+<C> and <CTRL>+<BREAK>...\n\n");
CtrlC(DISABLE); /* <<--*** CTRLCLIB Function *** */
while(1)
{
i = QueryCtrlC(); /* <<--*** CTRLCLIB Function *** */
if (i == CTRLC) /* this is TRUE when either CTRL+C -or- CTRL+BREAK is hit */
{
printf(" You pressed the <CTRL>+<C> keys\n");
printf(" Press any key...\n");
continue;
}
else if (i == CTRLBREAK) /* this will NEVER be true (in this demo section) */
{
printf(" You pressed the <CTRL>+<BREAK> keys\n");
printf(" Press any key...\n");
continue;
}
else
if ( ! kbhit()) continue;
if ( (c = ReadChar(s)) == 0) /* read and print the char typed */
continue; /* (not a printable char) */
printf(s);
if (QUIT) break;
}
CtrlC(ENABLE); /* <<--*** CTRLCLIB Function *** */
if (c != 'Q')
{
printf("\n\n\n\nCTRL+C has been ENABLED; press any key ");
printf("to continue to the next test...\n\n");
getch();
}
return(c);
}
/*********************************************************************/
int TEST3(void)
{
static char far *txt[]={
"This test shows how the installed interrupt handlers set a flag in the \n",
"CTRLCLIB communication area to indicate which set of keys were pressed.\n",
"In the case of pres